package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.UserJobConfig;
import com.ruoyi.web.service.IUserJobConfigService;
import com.ruoyi.web.utils.IdWorker;
import com.ruoyi.web.vo.UserJobConfigVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户任务等级配置Controller
 *
 * @author ruoyi
 * @date 2023-01-04
 */
@RestController
@RequestMapping("/config")
public class UserJobConfigController extends BaseController
{
    @Autowired
    private IUserJobConfigService userJobConfigService;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询用户任务等级配置列表
     */
    @Anonymous
    @GetMapping("/list")
    public R<List<UserJobConfigVO>> list(UserJobConfig jobConfig)
    {
        List<UserJobConfig> list = userJobConfigService.selectUserJobConfigList(jobConfig);
        List<UserJobConfigVO> voList = new ArrayList<>();
        for (UserJobConfig config : list) {
            UserJobConfigVO vo = new UserJobConfigVO();
            BeanUtils.copyProperties(config,vo);
            vo.setReward(vo.getReward().multiply(new BigDecimal(100)).multiply(new BigDecimal(vo.getCount())));
            voList.add(vo);
        }
        return R.ok(voList);
    }

    /**
     * 导出用户任务等级配置列表
     */
    @PreAuthorize("@ss.hasPermi('web:config:export')")
    @Log(title = "用户任务等级配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserJobConfig userJobConfig)
    {
        List<UserJobConfig> list = userJobConfigService.selectUserJobConfigList(userJobConfig);
        ExcelUtil<UserJobConfig> util = new ExcelUtil<UserJobConfig>(UserJobConfig.class);
        util.exportExcel(response, list, "用户任务等级配置数据");
    }

    /**
     * 获取用户任务等级配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        UserJobConfig config = userJobConfigService.selectUserJobConfigById(id);
        UserJobConfigVO vo = new UserJobConfigVO();
        BeanUtils.copyProperties(config,vo);
        return success(vo);
    }

    /**
     * 新增用户任务等级配置
     */
    @PreAuthorize("@ss.hasPermi('web:config:add')")
    @Log(title = "用户任务等级配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserJobConfig userJobConfig)
    {
        userJobConfig.setId(idWorker.nextId());
        return toAjax(userJobConfigService.insertUserJobConfig(userJobConfig));
    }

    /**
     * 修改用户任务等级配置
     */
    @PreAuthorize("@ss.hasPermi('web:config:edit')")
    @Log(title = "用户任务等级配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserJobConfig userJobConfig)
    {
        return toAjax(userJobConfigService.updateUserJobConfig(userJobConfig));
    }

    /**
     * 删除用户任务等级配置
     */
    @PreAuthorize("@ss.hasPermi('web:config:remove')")
    @Log(title = "用户任务等级配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userJobConfigService.deleteUserJobConfigByIds(ids));
    }
}
